﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace Daphne.Controls
{
    /// <summary>
    /// Interaction logic for RecentFileList.xaml
    /// </summary>
    public partial class RecentFileListControl : UserControl
    {
        public RecentFileListControl()
        {
            InitializeComponent();

            this.Loaded += RecentFileListControl_Loaded;
            uxRecentFiles.MouseDoubleClick += uxRecentFiles_MouseDoubleClick;
        }

        void uxRecentFiles_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var obj = uxRecentFiles.SelectedValue;
            if (obj == null)
                return;
            FileRec rec = obj as FileRec;
            if (rec == null)
                return;

            if (OpenCommand.CanExecute(rec.FilePath) == true)
                OpenCommand.Execute(rec.FilePath);
        }

        public ICommand OpenCommand
        { get; set; }

        void RecentFileListControl_Loaded(object sender, RoutedEventArgs e)
        {
            var files = this.RecentFileList.RecentFiles
                .Select(f => new FileRec(f)).ToList();
            this.DataContext = files;
        }



        public RecentFileList RecentFileList
        { get; set; }

        private class FileRec
        {
            public string FilePath
            { get; set; }

            public FileRec(string filePath)
            {
                FilePath = filePath;
            }

            public override string ToString()
            {
                return System.IO.Path.GetFileName(this.FilePath);
            }
        }
    }
}
